
#include <stdio.h>
#include <string.h>
#include "gui_object.h"
#include "sys_stdio.h"

int lcd_show_content(struct gui_console *gs_console, struct lcd_display_tools *tools)
{
    struct gui_object *obj =gs_console->current;
    //sj_kprintf("------- object name  %s -------- \r\n",obj->name);
    if(is_entry_object(obj))//show first level content
    {
        if(obj->content)
        {
            obj->content(tools);
        }
    }
    else //show sub object
    {
//        sj_kprintf("current->children->self_index_in_parent:%d \r\n", current->children->self_index_in_parent);
//        sj_kprintf("current->display_sub_first->self_index_in_parent:%d\r\n", current->display_sub_first->self_index_in_parent);
//        sj_kprintf("  **child name  %s \r\n",obj->children->name);

        if(obj->object_type == GUI_OBJECT_TYPE_DIR)
        {
            sj_int8 num=obj->child_num;
            if(num>DISPLAY_MAX_OBJ)
            {
                num = DISPLAY_MAX_OBJ;
            }
            struct gui_object *dis_obj =obj->display_sub_first;
            sj_kprintf("  display_sub_first  %s \r\n",dis_obj->name);
            if(dis_obj==NULL)
            {
                return ;
            }
            for(sj_int8 i=0;i<num;i++)
            {
                if(strcmp(dis_obj->name,obj->children->name)==0)
                {
                    tools->display(0, i*2, dis_obj->name, 1);
                    sj_kprintf("#%s \r\n",dis_obj->name);
                    dis_obj = gui_object_get_next(dis_obj);
                    continue;
                }
                tools->display(0, i*2, dis_obj->name, 0);
                sj_kprintf("%s \r\n",dis_obj->name);
                dis_obj = gui_object_get_next(dis_obj);
            }
        }
        else
        {
            if(obj->content)
            {
                obj->content(tools);
            }
        }
    }
}

void gui_object_init(struct gui_object *obj,const char *name)
{
    list_init(&obj->peer);
    obj->child_iter.i_list=NULL;
    obj->child_iter.i_cur =NULL;
    obj->name=name;
    obj->child_num=0;
    obj->into_flag = 0;
    obj->parent=NULL;
    obj->content=NULL;
    obj->children=NULL;
    obj->display_sub_first=NULL;
    obj->object_type=GUI_OBJECT_TYPE_FILE;
}

void gui_object_add_content(struct gui_object *obj,gui_object_content *content)
{
    obj->content = content;
}

void gui_object_add_peer(struct gui_object *obj,struct gui_object *peer)
{
    list_add_tail(&obj->peer, &peer->peer);
}

void gui_object_add_child(struct gui_object *obj,struct gui_object *sub)
{
    if(obj->child_iter.i_list==NULL)
    {
        list_iter_init(&sub->peer, &obj->child_iter);
    }
    else
    {
        list_add_tail(&sub->peer, obj->child_iter.i_list);
    }
    sub->parent = obj;
    obj->children = sub;
    sub->self_index_in_parent =obj->child_num;
    obj->child_num++;
    obj->object_type=GUI_OBJECT_TYPE_DIR;
}

int gui_object_get_child_num(struct gui_object *obj)
{
    return obj->child_num;
}
//zyp
struct gui_object *gui_object_get_priv(struct gui_object *obj)//get same level previous object
{
    struct gui_object *priv_obj=NULL;
    struct list *now =&obj->peer;

    struct list *priv=list_prev(now);

    priv_obj = to_gui_object(priv);

    return priv_obj;
}
//zyp
struct gui_object *gui_object_get_next(struct gui_object *obj)//get same level next object
{
    struct gui_object *next_obj=NULL;
    struct list *now =&obj->peer;

    struct list *next=list_next(now);

    next_obj = to_gui_object(next);

    return next_obj;
}

struct gui_object *gui_object_get_child_next(struct gui_object *obj)//get next child next
{
    struct gui_object *child=NULL;
    struct list_iter *iter =&obj->child_iter;
    if(iter->i_list)
    {
        struct list *child_list=list_iter_next(iter);

        child = to_gui_object(child_list);
    }
    return child;
}

struct gui_object *gui_object_get_child_priv(struct gui_object *obj)//get previous child
{
    struct gui_object *child=NULL;
    struct list_iter *iter =&obj->child_iter;
    if(iter->i_list)
    {
        struct list *child_list=list_iter_prev(iter);

        child = to_gui_object(child_list);
    }
    return child;
}

int gui_object_into_child(struct gui_console *gs_console)//enter sub
{
    struct gui_object *current =gs_console->current;
    if(current->object_type == GUI_OBJECT_TYPE_FILE)//object_type is file, can not enter
    {
        return 0;
    }
    if(!is_entry_object(current) && current->children != NULL)
    {
            gs_console->current = gs_console->current->children;

            struct list_iter *iter = &gs_console->current->child_iter;
            list_iter_exit(iter);   //clear sub list
            gs_console->current->children =\
                    gui_object_get_child_next(gs_console->current);//get first sub

            init_sub_first_display_obj(gs_console->current->display_sub_first,gs_console->current->children);
    }
//    else if(current->children->object_type == GUI_OBJECT_TYPE_FILE && current->into_flag == 0)
//    {
//        current->into_flag = 1;
//        sj_kprintf("set/get %s \r\n", current->children->name);
//    }
}


int gui_object_go_back_to_parent(struct gui_console *gs_console)
{
    struct gui_object *current =gs_console->current;
    if(is_entry_object(gs_console->current))//enter menu object
    {
        gs_console->current = current->children;
        struct list_iter *iter =&gs_console->current->child_iter;
        list_iter_exit(iter);   //clear sub list
        gs_console->current->children =\
                gui_object_get_child_next(gs_console->current);//get first sub

        init_sub_first_display_obj(gs_console->current->display_sub_first,gs_console->current->children);

    }
    else if(gs_console->current->into_flag == 1)
    {
        current->into_flag = 0;
    }
    else    //go back parent object
    {
        current->display_sub_first=NULL;
        gs_console->current = current->parent;

    }
}

int gui_object_go_peer_next(struct gui_console *gs_console)
{
    struct gui_object *current =gs_console->current;
    if(current->object_type == GUI_OBJECT_TYPE_FILE)//object_type is file, can not up or down
    {
        return 0;
    }
    if(is_entry_object(gs_console->current))
    {
        gs_console->current =gui_object_get_next(current);
    }
    else
    {
        current->children = gui_object_get_child_next(current);
        sj_int8 current_child_from_first=
                current->children->self_index_in_parent -current->display_sub_first->self_index_in_parent;

        if(current_child_from_first<=-4 || (current_child_from_first > 0 && current_child_from_first < (current->child_num - 3)))
        {
            current->display_sub_first = gui_object_get_next(current->display_sub_first);
        }
    }
}

int gui_object_go_peer_priv(struct gui_console *gs_console)
{
    struct gui_object *current =gs_console->current;
    if(current->object_type == GUI_OBJECT_TYPE_FILE)//object_type is file, can not up or down
    {
        return 0;
    }
    if(is_entry_object(current))
    {
        gs_console->current =gui_object_get_priv(current);
    }
    else
    {
        current->children = gui_object_get_child_priv(current);
        sj_int8 current_child_from_first=
                current->children->self_index_in_parent -current->display_sub_first->self_index_in_parent;

        if(current_child_from_first <= -4 || (current_child_from_first > 0 && current_child_from_first < (current->child_num - 3)))
        {
            current->display_sub_first = gui_object_get_priv(current->display_sub_first);
        }
    }
}

